﻿using UnityEngine;

public class ItemListManagerVertitcal : MonoBehaviour
{
    public float startPos;
    public float itemWith = 1080;
    //public float space;

    public float disJudgeMax = 5f;
    public float disJudgeMin = .01f;

    public bool inLerping;

    public int maxIndex = 10;

    public int curIndex;

    public int showingIndex;
    // Start is called before the first frame update
    void Start()
    {

    }

    bool m_Moving;
    private Vector3 m_OldPos;

    public void ShowIndex(int index)
    {
        curIndex = showingIndex =  index;

        float targetPos = startPos + showingIndex * (itemWith);

        var maxPos = this.maxIndex * itemWith - startPos;

        float targetY = Mathf.Clamp(targetPos, startPos, maxPos);

        var localPos = transform.localPosition;
        localPos.y = targetY;

        transform.localPosition = localPos;

        GameDebug.LogError($"ShowIndex:{index}");
    }

    // Update is called once per frame
    void Update()
    {
        var vec = transform.localPosition - m_OldPos;

        var localPos = transform.localPosition;
        var y = localPos.y;

        showingIndex = (int)((y - startPos) / itemWith);

        if (vec.y < 0)
        {
            float xdis = (y - startPos) - showingIndex * itemWith;
            if (Mathf.Abs(xdis) > Mathf.Abs(itemWith / 2))
                showingIndex += 1;
        }
        else
        {
            float xdis = (y - startPos) - showingIndex * itemWith;
            if (Mathf.Abs(xdis) > Mathf.Abs(itemWith / 2))
                showingIndex += 1;
        }

        if (Input.GetMouseButtonUp(0))
        {
            m_Moving = true;
        }

        if (m_Moving)
        {
            float targetPos = startPos + showingIndex * (itemWith);

            float targetY = Mathf.Lerp(y, targetPos, .5f);

            var maxPos = this.maxIndex * itemWith - startPos;

            targetY = Mathf.Clamp(targetY, startPos, maxPos);

            localPos.y = targetY;

            transform.localPosition = localPos;

            if(Mathf.Abs(transform.localPosition.y - targetPos) < 0.01f)
            {
                m_Moving = false;

                localPos.y = targetPos;
                transform.localPosition = localPos;
            }
            curIndex = showingIndex;
        }


        //float dis = vec.magnitude;
        //if (dis > disJudgeMin && dis < disJudgeMax)
        //{
        //    //var y = localPos.y;

        //    if (!inLerping)
        //    {
        //        inLerping = true;

        //        curIndex = (int)((y - startPos) / itemWith);

        //        if (vec.y < 0)
        //        {
        //            float xdis = (y - startPos) - curIndex * itemWith;
        //            if (Mathf.Abs(xdis) > Mathf.Abs(itemWith / 2))
        //                curIndex += 1;
        //        }
        //        else
        //        {
        //            float xdis = (y - startPos) - curIndex * itemWith;
        //            if (Mathf.Abs(xdis) > Mathf.Abs(itemWith / 2))
        //                curIndex += 1;
        //        }
        //    }

        //    float targetPos = startPos + curIndex * (itemWith);

        //    float targetY = Mathf.Lerp(y, targetPos, .2f);

        //    var maxPos = this.maxIndex * itemWith - startPos;

        //    targetY = Mathf.Clamp(targetY, startPos, maxPos);
        //    //targetX = Mathf.Clamp(targetX, startPos,maxPos );

        //    localPos.y = targetY;

        //    transform.localPosition = localPos;
        //}
        //else
        //{
        //    inLerping = false;
        //}

        m_OldPos = transform.localPosition;
    }
}